package com.gp.gyx.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.gp.entity.Result;
import com.gp.entity.SDeviceElectric;
import com.gp.gyx.dao.DataInDao;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2024/4/26 10:33
 * @Author BigPotato743
 * @Email 2764826228@qq.com
 * @Phone 17623386574
 */

@ResponseBody
@CrossOrigin
@RestController
@RequestMapping("/data")
public class DataIn {
    @Resource
    private DataInDao dao;

    private static int num = 0;

    @PostMapping("/in")
    private Result dataIn(@RequestParam("file") MultipartFile file){
        List<SDeviceElectric> list = new ArrayList<>();
        try {
            InputStream inputStream = file.getInputStream();
            list = EasyExcel.read(inputStream).head(SDeviceElectric.class).excelType(ExcelTypeEnum.CSV).sheet().doReadSync();
            list.forEach(a->{
                if (!"".equals(a.getTs()) || a.getTs() != null){
                    a.setTs(a.getTs().substring(0,19));
                }
            });
            ArrayList<SDeviceElectric> temp = new ArrayList<>();
            for (int i = 0 ;i<list.size() ;i++){
                temp.add(list.get(i));
                if (temp.size()==10000){
                    dao.datain(temp);
                    temp.clear();
                    System.out.println("完成了第"+(i-9999)+"行到第"+i+"行数据的导入");
                }
            }
            dao.datain(temp);
            System.out.println("导入完成！");


        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.success(list);
    }
}
